package cn.way.unity;

import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.LogUtils;
import com.unity3d.player.UnityPlayer;



/**
 * @author :  wei Wang <wei.wang@ecarx.com.cn>
 * @ClassName: cn.way.unity.UnityActivity
 * @date 2021/7/14 16:49
 * @Description:
 */
public class UnityActivity extends AppCompatActivity {

    protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code

    private int param = 1;

    private boolean pause = false;
    // Setup activity layout
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_unity);
        mUnityPlayer = new UnityPlayer(this);
        FrameLayout container = findViewById(R.id.container);
        container.addView(mUnityPlayer);
//        setContentView(mUnityPlayer);
        mUnityPlayer.requestFocus();
    }

    // Quit Unity
    @Override protected void onDestroy ()
    {
        mUnityPlayer.destroy();
        super.onDestroy();
    }

    // Pause Unity
    @Override protected void onPause()
    {
        super.onPause();
        mUnityPlayer.pause();
    }

    // Resume Unity
    @Override protected void onResume()
    {
        super.onResume();
        mUnityPlayer.resume();
    }

    @Override protected void onStart()
    {
        super.onStart();
        mUnityPlayer.start();
    }

    public void androidMethod() {
        Toast.makeText(UnityActivity.this, "unity call android", Toast.LENGTH_LONG).show();
    }

    public void androidMethod(String args) {
        Toast.makeText(UnityActivity.this, String.format("param from unity: %s", args), Toast.LENGTH_LONG).show();
    }

    @Override protected void onStop()
    {
        super.onStop();
        mUnityPlayer.stop();
    }

    // Low Memory Unity
    @Override public void onLowMemory()
    {
        super.onLowMemory();
        mUnityPlayer.lowMemory();
    }

    // Trim Memory Unity
    @Override public void onTrimMemory(int level)
    {
        super.onTrimMemory(level);
        if (level == TRIM_MEMORY_RUNNING_CRITICAL)
        {
            mUnityPlayer.lowMemory();
        }
    }

    // This ensures the layout will be correct.
    @Override public void onConfigurationChanged(Configuration newConfig)
    {
        super.onConfigurationChanged(newConfig);
        mUnityPlayer.configurationChanged(newConfig);
    }

    // Notify Unity of the focus change.
    @Override public void onWindowFocusChanged(boolean hasFocus)
    {
        super.onWindowFocusChanged(hasFocus);
        mUnityPlayer.windowFocusChanged(hasFocus);
    }

    public void start(View view){
//        UnityPlayer.UnitySendMessage("Unity中的GameObject","GameObject对应脚本中的函数名","传递到Unity中的string");
        UnityPlayer.UnitySendMessage("AndroidProxy","dancing", String.valueOf(param++));

    }

    public void pause(View view){
        LogUtils.i("pause");
        if (pause){
            mUnityPlayer.start();
            mUnityPlayer.resume();
        }else {
            mUnityPlayer.pause();
        }
        pause = !pause;
        TextView textView = (TextView) view;
        textView.setText(pause ? "resume" : "pause");
    }

    public void back(View view){
        LogUtils.i("back");
        finish();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        LogUtils.i(String.format("on touch, action: %d", event.getAction()));
        if (event.getAction() == MotionEvent.ACTION_UP){
            LogUtils.i(String.format("on touch, x: %.2f, y: %.2f", event.getX(), event.getY()));
        }
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }
}
